package com.vancouver.knows.business.tracker;

import android.util.Log;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.FetchUserBusinessImagesTaskMark;

import java.util.List;

/**
 * Created by Chris Li on 2016-08-24.
 */
public class FetchUserBusinessImagesTracker extends ABInvokeTracker {

    public FetchUserBusinessImagesTracker(BusinessModule module, IResultReceiver resultReceiver) {
        super(module, resultReceiver);
    }

    @Override
    protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
        module.getCacheManager().getUserBusinessImages().clear();
    }

    @Override
    public String TAG() {
        return FetchUserBusinessImagesTracker.class.getSimpleName();
    }

    @Override
    public void handleResult(OperateResult result) {
        PageableResult pageableResult = (PageableResult) result.getResultData();
        if (pageableResult.getContent() != null) {
            List<String> images = (List<String>) pageableResult.getContent();
            List<String> cachedImages = module.getCacheManager().getUserBusinessImages();
            cachedImages.addAll(images);
            module.getCacheManager().setUserBusinessImagesList(cachedImages);

            FetchUserBusinessImagesTaskMark fetchUserBusinessImagesTaskMark = (FetchUserBusinessImagesTaskMark) result.getTaskMark();
            fetchUserBusinessImagesTaskMark.setPageInfo(pageableResult.getPageInfo());
        }
    }
}
